//
//  VMCModuleBar.m
//  vmcshop
//
//  Created by bigknife on 14-10-9.
//  Copyright (c) 2014年 idongler. All rights reserved.
//

#import "VMCModuleBar.h"
#import "UIView+Borders.h"
#import "ColorUtils.h"
#import "VMCConstants.h"
#import "VMCTabButton.h"

@interface VMCModuleBar()
@property (strong, nonatomic) IBOutlet UIView *container;
@property (strong, nonatomic) IBOutlet VMCTabButton *btnIndexPage;
@property (strong, nonatomic) IBOutlet VMCTabButton *btnCategory;
@property (strong, nonatomic) IBOutlet VMCTabButton *btnCart;
@property (strong, nonatomic) IBOutlet VMCTabButton *btnMemberCenter;
//@property (strong, nonatomic) IBOutlet UIToolbar *bgToolbar;

@property (strong, nonatomic) NSArray *items;
@end

@implementation VMCModuleBar


-(void)awakeFromNib
{
    [super awakeFromNib];
   
    self.backgroundColor = [UIColor whiteColor];
	self.container.backgroundColor = [UIColor whiteColor];
    [[NSBundle mainBundle] loadNibNamed:@"VMCModuleBar" owner:self options:nil];
    
    [self addSubview:self.container];
    
    //add constraints to self.container
    self.container.translatesAutoresizingMaskIntoConstraints = NO;
    UIView *container = self.container;
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(container);
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[container]-0-|" options:0 metrics:nil views:viewsDictionary];
    [self addConstraints:constraints];
    
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[container]-0-|" options:0 metrics:nil views:viewsDictionary];
    [self addConstraints:constraints];
    
    //add one px top border
    [self addTopBorderWithHeight:1/[UIScreen mainScreen].scale andColor:[UIColor colorWithString:@"#dcdcdc"]];
   
    
    self.btnCart.sTag = (NSString *) kVMCModuleTagCart;
    self.btnCategory.sTag = (NSString *)kVMCModuleTagCategory;
    self.btnIndexPage.sTag = (NSString *) kVMCModuleTagIndexPage;
    self.btnMemberCenter.sTag = (NSString *)kVMCModuleTagMemberCenter;
	
    [self.btnIndexPage addTarget:self action:@selector(handleItemClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.btnCart addTarget:self action:@selector(handleItemClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.btnCategory addTarget:self action:@selector(handleItemClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.btnMemberCenter addTarget:self action:@selector(handleItemClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    self.items = @[self.btnIndexPage, self.btnCategory, self.btnCart,self.btnMemberCenter];
    
    
    if (OSVersionLessThan7)
	{
        //[self.bgToolbar removeFromSuperview];
    }else
	{
        self.backgroundColor = [UIColor whiteColor];
    }
}

-(void) handleItemClicked:(VMCTabButton *)sender
{
    for (VMCTabButton *btn in self.items)
 {
        if (sender == btn) {
            if ([self.delegate respondsToSelector:@selector(moduleBar:willSelectItemWithTag:)]) {
                if ([self.delegate moduleBar:self willSelectItemWithTag:btn.sTag]) {
                    if ([self.delegate respondsToSelector:@selector(moduleBar:didSelectItemWithTag:)]) {
                        [self.delegate moduleBar:self didSelectItemWithTag:btn.sTag];
                    }
                }
            }else if ([self.delegate respondsToSelector:@selector(moduleBar:didSelectItemWithTag:)]) {
                [self.delegate moduleBar:self didSelectItemWithTag:btn.sTag];
            }
            break;
        }
    }
}

-(void) setSelectedSTag:(NSString *)selectedSTag {
    _selectedSTag = selectedSTag;
    
    for (VMCTabButton *btn in self.items) {
        if ([selectedSTag isEqualToString:btn.sTag]) {
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
}
@end
